package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.common.AreaVo;
import com.lemon.smartsafety.entity.common.CommonVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;


/**
 * Created by Cooliean on 16/1/19.
 * 地区选择
 */
public class CommonServices extends BaseHttpServices {

    private LoggerHelper loggerHelper = new LoggerHelper(CommonServices.class);

    private CommonServices(Context context) {
        super(context);
    }

    public static CommonServices init() {
        return new CommonServices(IApplication.context());
    }


    /**
     * 获取地域信息 - 省份
     *
     * @return
     */
    public CommonServices getProvinceInfo() {
        LemonRequestParams params = new LemonRequestParams();
        return getAreaInfo(params, EventTag.TAG_AREA_PROVINCE, UrlInfo.AREA_PROVINCE);
    }


    /**
     * 获取地域信息 - 市(不是给注册界面用）
     *
     * @return
     */
    public CommonServices getCityInfo(String parentId) {
        LemonRequestParams params = new LemonRequestParams();
        return getCityInfo(parentId, false);
    }


    /**
     * 获取地域信息 - 市
     *
     * @return
     */
    public CommonServices getCityInfo(String parentId, boolean isRegister) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("parent_id", parentId);
        params.put("register", isRegister ? "1" : "0");
        return getAreaInfo(params, EventTag.TAG_AREA_CITY, UrlInfo.AREA_CITY);
    }


    /**
     * 获取地域信息 - 区
     *
     * @return
     */
    public CommonServices getDistrictInfo(String parentId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("parent_id", parentId);
        return getAreaInfo(params, EventTag.TAG_AREA_DISTRICT, UrlInfo.AREA_DISTRICT);
    }


    /**
     * 获取地域信息 - 街道
     *
     * @return
     */
    public CommonServices getStreetInfo(String parentId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("parent_id", parentId);
        return getAreaInfo(params, EventTag.TAG_AREA_STREET, UrlInfo.AREA_STREET);
    }


    /**
     * 获取地域信息 - 乡村
     *
     * @return
     */
    public CommonServices getVillageInfo(String parentId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("parent_id", parentId);
        return getAreaInfo(params, EventTag.TAG_AREA_VILLAGE, UrlInfo.AREA_VILLAGE);
    }


    public CommonServices getAreaInfo(String parentId, String tag, String url) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("parent_id", parentId);
        return getAreaInfo(params, tag, url);
    }


    /**
     * 获取检查项目的检查流程数据
     *
     * @param params
     * @return
     */
    private CommonServices getAreaInfo(LemonRequestParams params, String tag, String url) {
        HandlerCallback<List<AreaVo>> callback = new HandlerCallback<List<AreaVo>>(tag) {
        };
        callback.setOther(RefreshType.REFRESH);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(url);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取行业
     */
    public void getIndustryList() {
        LemonRequestParams params = new LemonRequestParams();
        HandlerCallback<List<CommonVo>> callback = new HandlerCallback<List<CommonVo>>(EventTag.TAG_INDUSTRY) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.INDUSTRY_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }

}
